#include "task_app.h"
#include "config.h"
#include "encoder_app.h"
#include "read_table.h"
#include "delay.h"

Task_Prio_t Task_Prio = 
{
  .flag = PRIO_READ_TABLE
};

/*********************************************************************************
�������� �� Read_Table_Task
��    �� �� ����������
��    �� �� ��
�� �� ֵ �� ��
˵    �� �� ��
**********************************************************************************/
bool Read_Table_Task(void)
{  
  static uint8_t send_flag = 0;
  static uint8_t time_count = 0, no_ack_count = 0;
  
  if(Analysis_Read_Table_Data(&Uart_Read_Table, &Uart0_Buffer, &Uart0_Data))
  {
    no_ack_count = 0;
    time_count = 0;
    send_flag = 0;
    
    return false;
  }
  else
  {
    if(!send_flag)
    {
      send_flag = 1;
      Send_Read_Table_Cmd();//��ȡ������
      
      if(++no_ack_count >= 3)
      {
        no_ack_count = 0;
        time_count = 0;
        send_flag = 0;
        
        Delay_ms(10);//�ȴ����ݷ������
        
        return false;
      }
    }
    else 
    {
      if(++time_count >= 40)
      {
        time_count = 0;
        send_flag = 0;
      }
    }
  }
  
  Delay_ms(10);//10MS��ѵһ��
  
  return true;
}

/*********************************************************************************
�������� �� Encoder_Task
��    �� �� Encoder������
��    �� �� ��
�� �� ֵ �� ��
˵    �� �� ��
**********************************************************************************/
bool Encoder_Task(void)
{
  if(Encoder_Receive_Pulse_Deal(&Encoder_Data))
  {
    return false;
  }

  return true;
}

/*********************************************************************************
�������� �� Config_Task
��    �� �� ����������
��    �� �� ��
�� �� ֵ �� ��
˵    �� �� ��
**********************************************************************************/
bool Config_Task(void)
{
  static uint8_t first_rx_flag = 0;
  static uint16_t time_count = 0;
  
  if(!first_rx_flag)
  {
    first_rx_flag = 1;
    Delay_ms(50);
    Uart0_Buffer.rx = 0;//���ջ��泤������
  }
  
  if(Analysis_Config_Data(&System_Config, &Uart0_Buffer, &Uart0_Data))
  {
    time_count = 0;//���¿�ʼ��ʱ
  }
  else 
  {
    //����5��δ�յ���ȷ���ݣ�����͹���
    if(++time_count >= 500)
    {
      time_count = 0;
      first_rx_flag = 0;
      
      return false;
    }
  }
  
  Delay_ms(10);//10MS��ѵһ��
  
  return true;
}

/*********************************************************************************
�������� �� Query_Prio
��    �� �� �������ȼ�
��    �� �� ��
�� �� ֵ �� ��
˵    �� �� ��
**********************************************************************************/
bool Query_Prio(Task_Prio_t *p_task)
{
  switch(p_task->flag)
  {
    case PRIO_READ_TABLE:
        {
          if(!Read_Table_Task())
          {
            p_task->flag = PRIO_IDLE;
            return false;
          }
          else return true;
        }break;
    case PRIO_ENCODER:
        {
          if(!Encoder_Task())
          {
            p_task->flag = PRIO_IDLE;
            return false;
          }
          else return true;
        }break;
    case PRIO_CONFIG:
        {
          if(!Config_Task())
          {
            p_task->flag = PRIO_IDLE;
            return false;
          }
          else return true;
        }break;
    default:return false;
  }
}

void test1_demo(void)
{
  
}





